package com.marketingexpert.controller.publicmgnt.tx;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.service.snsmgnt.RequestToken;
import com.marketingexpert.service.snsmgnt.SnsService;
import com.marketingexpert.service.snsmgnt.SnsServiceFactory;
import com.marketingexpert.util.WebUtils;

public class TencentAuthController implements Controller {

	private SnsServiceFactory snsServiceFactory;
	
	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		SnsService snsService = snsServiceFactory.getService(SnsServiceFactory.TECENT);
		
		RequestToken resToken = snsService.getRequestToken(WebUtils.getBasepath(request) + "/unsec/t/callback.do");
		
		if(resToken != null) {
			request.getSession().setAttribute("resToken", resToken);
			response.sendRedirect(resToken.getAuthorizationURL());
			return null;
		} else {
			response
			.sendRedirect(WebUtils.getBasepath(request)
					+ "/unsec/t/search.do?pCat=0&qCat=00&page=1&catCur=sAll&cur=li_all");
			return null;
		}
	}

	public SnsServiceFactory getSnsServiceFactory() {
		return snsServiceFactory;
	}

	public void setSnsServiceFactory(SnsServiceFactory snsServiceFactory) {
		this.snsServiceFactory = snsServiceFactory;
	}
}
